<script>
import FloatingConfigurator from '@/components/FloatingConfigurator.vue';
import { useToast } from 'primevue/usetoast';
import AuthService from '@/service/AuthService';

export default {
    name: 'LoginPage',
    components: {
        FloatingConfigurator
    },
    data() {
        return {
            email: '',
            password: '',
            checked: true,
            loading: false,
            authService: new AuthService()
        };
    },
    methods: {
        async login() {
            if (!this.email || !this.password) {
                this.$toast.add({
                    severity: 'error',
                    summary: '输入错误',
                    detail: '请输入邮箱和密码',
                    life: 3000
                });
                return;
            }

            this.loading = true;

            try {
                await this.authService.login(this.email, this.password);

                // 登录成功，跳转到首页
                this.$router.push('/');

                this.$toast.add({
                    severity: 'success',
                    summary: '登录成功',
                    detail: '欢迎回来！',
                    life: 3000
                });
            } catch (error) {
                console.error('登录失败:', error);
                this.$toast.add({
                    severity: 'error',
                    summary: '登录失败',
                    detail: error.message || '用户名或密码错误',
                    life: 3000
                });
            } finally {
                this.loading = false;
            }
        }
    },
    setup() {
        const toast = useToast();
        return { toast };
    }
};
</script>

<template>
    <FloatingConfigurator />
    <div class="bg-surface-50 dark:bg-surface-950 flex items-center justify-center min-h-screen min-w-[100vw] overflow-hidden">
        <div class="flex flex-col items-center justify-center">
            <div style="border-radius: 56px; padding: 0.3rem; background: linear-gradient(180deg, var(--primary-color) 10%, rgba(33, 150, 243, 0) 30%)">
                <div class="w-full bg-surface-0 dark:bg-surface-900 py-20 px-8 sm:px-20" style="border-radius: 53px">
                    <div class="text-center mb-8 items-center justify-center flex flex-col">
                        <img class="h-32 w-32" src="@/assets/logo.png"/>
                        <div class="text-surface-900 dark:text-surface-0 text-3xl font-medium mb-4">Welcome to HQBA!</div>
                        <span class="text-muted-color font-medium">Sign in to continue</span>
                    </div>

                    <div>
                        <label for="email1" class="block text-surface-900 dark:text-surface-0 text-xl font-medium mb-2">邮箱</label>
                        <InputText id="email1" type="text" placeholder="请输入邮箱" class="w-full md:w-[30rem] mb-8" v-model="email" />

                        <label for="password1" class="block text-surface-900 dark:text-surface-0 font-medium text-xl mb-2">密码</label>
                        <Password id="password1" v-model="password" placeholder="请输入密码" :toggleMask="true" class="mb-4" fluid :feedback="false" @keyup.enter="login"></Password>

                        <div class="flex items-center justify-between mt-2 mb-8 gap-8">
                            <div class="flex items-center">
                                <Checkbox v-model="checked" id="rememberme1" binary class="mr-2"></Checkbox>
                                <label for="rememberme1">记住我</label>
                            </div>
                            <span class="font-medium no-underline ml-2 text-right cursor-pointer text-primary">忘记密码?</span>
                        </div>
                        <Button label="登录" class="w-full" :loading="loading" @click="login"></Button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <Toast />
</template>

<style scoped>
.pi-eye {
    transform: scale(1.6);
    margin-right: 1rem;
}

.pi-eye-slash {
    transform: scale(1.6);
    margin-right: 1rem;
}
</style>
